﻿//定义js命名空间
Ext.namespace("Infrastructure");

Infrastructure.UserRoleDept = function () {

    // 机构用户
    var gridStoreUser = new Ext.data.Store({
        url: "/InfraUserRoleDep/QueryUserListByCode",
        remoteSort: true,
        reader: new Ext.data.JsonReader({
            root: 'data',
            id: 'Id',
            totalProperty: 'total'
        }, [{
            name: 'Id',
            mapping: 6,
            type: 'string'
        }, {
            name: 'Person.Id',
            mapping: 0,
            type: 'string'
        }, {
            name: 'Person.LoginName',
            mapping: 1,
            type: 'string'
        }, {
            name: 'Person.Name',
            mapping: 2,
            type: 'string'
        }, {
            name: 'Role.Id',
            mapping: 3,
            type: 'string'
        }, {
            name: 'Role.Code',
            mapping: 4,
            type: 'string'
        }, {
            name: 'Role.Name',
            mapping: 5,
            type: 'string'
        }, {
            name: 'Department.Id',
            mapping: 7,
            type: 'string'
        }, {
            name: 'Department.Code',
            mapping: 8,
            type: 'string'
        }, {
            name: 'Department.Name',
            mapping: 9,
            type: 'string'
        }]
        )
    });
    gridStoreUser.setDefaultSort('Person.Code', 'ASC');

    // 用户Filter
    var UserGirdFilter = new Ext.ux.grid.GridFilters({
        encode: true,
        filters: [
        {
            type: 'string',
            dataIndex: 'Department.Code'
        }, {
            type: 'string',
            dataIndex: 'Department.Name'
        }, {
            type: 'string',
            dataIndex: 'Person.LoginName'
        }, {
            type: 'string',
            dataIndex: 'Person.Name'
        }, {
            type: 'string',
            dataIndex: 'Role.Code'
        }, {
            type: 'string',
            dataIndex: 'Role.Name'
        }]
    });

    // 用户Selectmode
    var UserSM = new Ext.grid.CheckboxSelectionModel();

    // 用户表格列
    var UserCM = new Ext.grid.ColumnModel([
	    new Ext.grid.RowNumberer(), UserSM, {
	        header: "登录名",
	        dataIndex: 'Person.LoginName',
	        width: 60,
	        sortable: true
	    }, {
	        header: "用户名称",
	        dataIndex: 'Person.Name',
	        width: 80,
	        sortable: true
	    }, {
	        header: "机构代码",
	        dataIndex: 'Department.Code',
	        width: 60,
	        sortable: true
	    }, {
	        header: "机构名",
	        dataIndex: 'Department.Name',
	        width: 60,
	        sortable: true
	    }, {
	        header: "角色代码",
	        dataIndex: 'Role.Code',
	        width: 60,
	        sortable: true
	    }, {
	        header: "角色名称",
	        dataIndex: 'Role.Name',
	        sortable: true
	    }
    ]);

    // 用户角色表格工具栏
    var UserTBar = [{
        xtype: "tbbutton",
        minWidth: 80,
        ref: '../addBtn',
        text: "添加项",
        cls: "x-btn-text-icon",
        icon: "/Content/icons/add.png",
        handler: function (btn, e) {
            showUserRoleDepEditFormWinFn("添加用户权限");
        }
    }, "-", {
        xtype: "tbbutton",
        ref: '../delBtn',
        minWidth: 80,
        disabled: true,
        text: "删除项",
        cls: "x-btn-text-icon",
        icon: "/Content/icons/delete.png",
        handler: function (btn, e) {
            JsHelper.DelConfirm(function (btn) {
                if (btn == 'yes') {
                    var records = UserGrid.getSelectionModel().getSelections();
                    for (var i = 0; i < records.length; i++) {
                        Ext.Ajax.request({
                            url: '/InfraUserRoleDep/Delete',
                            params: {
                                'Id': records[i].get("Id")
                            },
                            success: function (response, opts) {
                                var result = Ext.util.JSON.decode(response.responseText);
                                if (result.success == false) {
                                    JsHelper.ShowError(result.msg);
                                } else {
                                    var paramsId = opts.params.Id;
                                    var r = gridStoreUser.getAt(gridStoreUser.find("Id", paramsId));
                                    if (Ext.isDefined(r)) {
                                        gridStoreUser.remove(r);
                                    }
                                }
                            }
                        });
                    }
                }
            });
        }
    }];

    // 用户授权窗口
    var showUserRoleDepEditFormWinFn = function (title) {
        // 机构清单
        var DeptStore = new Ext.data.JsonStore({
            url: '/InfraHospUser/GetDeptList',
            root: 'data',
            totalProperty: 'total',
            id: 'Id',
            fields: [{
                name: 'Id',
                type: 'string'
            }, {
                name: 'Name',
                type: 'string'
            }, {
                name: 'Code',
                type: 'string'
            }]
        });

        // 角色清单        
        var RoleStore = new Ext.data.JsonStore({
            url: '/InfraRole/GetPlist',
            root: 'data',
            totalProperty: 'total',
            id: 'Id',
            fields: [{
                name: 'Id',
                type: 'string'
            }, {
                name: 'Name',
                type: 'string'
            }, {
                name: 'Code',
                type: 'string'
            }]
        });

        // 用户
        var UserStore = new Ext.data.JsonStore({
            url: '/InfraHospUser/GetPlist',
            root: 'data',
            totalProperty: 'total',
            id: 'Id',
            fields: [{
                name: 'Id',
                type: 'string'
            }, {
                name: 'Name',
                type: 'string'
            }, {
                name: 'Code',
                type: 'string'
            }]
        });

        // 用户授权Form
        var form_AddUserRoleDep = new Ext.FormPanel({
            labelWidth: 75, // label settings here cascade unless overridden
            frame: true,
            border: false,
            bodyStyle: 'padding:5px 5px 0',
            labelAlign: 'right',
            items: [{
                fieldLabel: '用户<span style="color: red;" >*</span>',
                xtype: 'idccombo',
                name: 'Name',
                hiddenName: 'UserID',
                store: UserStore,
                width: 150,
                listWidth: 200
            }, {
                fieldLabel: '机构<span style="color: red;" >*</span>',
                xtype: 'idccombo',
                name: 'Name',
                hiddenName: 'DepartmentID',
                store: DeptStore,
                width: 150,
                listWidth: 200
            }, {
                fieldLabel: '角色<span style="color: red;" >*</span>',
                xtype: 'idccombo',
                name: 'Name',
                hiddenName: 'RoleID',
                store: RoleStore,
                width: 150,
                listWidth: 200
            }],
            buttonAlign: 'center',
            buttons: [new Ext.ux.SubmitBtn({
                handler: function () {
                    if (form_AddUserRoleDep.getForm().isValid()) {
                        form_AddUserRoleDep.getForm().submit({
                            url: '/InfraUserRoleDep/SaveUserDepRole',
                            success: function (form, action) {
                                Ext.MessageBox.alert('提示', '用户角色添加成功!');
                                gridStoreUser.load();   //刷新表格
                                win.destroy(); // 关闭窗口
                            },
                            failure: function (form, action) {
                                JsHelper.ShowError(action.result.msg);
                            }
                        });
                    }
                }
            }), new Ext.ux.CancelBtn({
                handler: function () {
                    win.destroy();
                }
            })]
        });

        // 定义显示窗口
        var win = new Ext.Window({
            title: title,
            iconCls: 'locked',
            width: 300,
            resizable: false,
            constrain: true,
            autoHeight: true,
            modal: true,
            closeAction: 'close',
            plain: true,
            items: [form_AddUserRoleDep]
        });

        //显示窗口
        win.show();
    };

    // 分页
    var UserBBar = new Ext.PagingToolbar({
        displayInfo: true,
        emptyMsg: "没有数据显示",
        displayMsg: "{0}到{1}条，共{2}条",
        store: gridStoreUser,
        pageSize: ALL_PAGESIZE_SETTING,
        plugins: [UserGirdFilter]
    })

    // 表格
    var UserGrid = new Ext.grid.GridPanel({
        //region: "center",
        enableColumnMove: false,
        sm: UserSM,
        cm: UserCM,
        plugins: [UserGirdFilter],
        stripeRows: true,
        frame: true,
        border: true,
        layout: "fit",
        store: gridStoreUser,
        loadMask: true,
        tbar: UserTBar,
        bbar: UserBBar
    });

    UserGrid.getSelectionModel().on('selectionchange', function (sm) {
        UserGrid.delBtn.setDisabled(sm.getCount() < 1);
    });

    // 返回给对外的函数
    return {
        UserRoleDeptStore: function () {
            return gridStoreUser;
        },

        UserRoleDeptGrid: function () {
            return UserGrid;
        }
    }

} ();

